<?php
namespace Club\Controller;
use Think\Controller;

class FundingController extends \Org\Util\AdminController {



    /**
     * 权限验证标志位,默认false
     * false:所有action都需要权限验证;
     * true:所有action都不需要权限验证;
     * array:array中的action不需要权限验证，其余需要。
     */
    protected $notAuthority = false;


    /**
     * 创建之后做的事情
     */
    public function interfaceAfterCreate($id) {
        //创建一个具体筹单
        $funding = D('Funding')->find($id);
        $fundingType = D('FundingType')->find($funding['funding_type'])['en'];
        $fundingType = 'Funding'.ucfirst($fundingType);
        unset($data);
        $data['principal_id'] = $id;
        D($fundingType)->create($data);
        $fundingSpecifyId = D($fundingType)->add();
        //给定制订单一个筹单id，并把定制订单设置为待支付
        if ($funding['transaction_type'] == '购买') {
            D('CustomizedTravel')->where(['id'=>$_POST['customized_id']])->data(['funding_id'=>$id, 'club_user_id'=>session('旅社')['id'],'state'=>'待发起','update_time'=>time()])->save();
        } else {
            //定制订单状态
            //总人数计算
            $totalNum = D("TouristOrder")->where(["order_type"=>"customized_travel", "order_id"=>$_POST['customized_id']])->count();
            //更新筹单总人数
            D('Funding')->where(['id'=>$id])->data(['total_num'=>$totalNum, 'already_num'=>$totalNum])->save();
            D('CustomizedTravel')->where(['id'=>$_POST['customized_id']])->data(['funding_id'=>$id, 'club_user_id'=>session('旅社')['id'],'state'=>'待签署合同','update_time'=>time()])->save();
        }
        //通知用户
        $order = D('CustomizedTravel')->where(['id'=>$_POST['customized_id']])->find();
        notice(1, $order['user_id'], '系统通知', '定制旅游收到反馈，快去查看吧');
        //通知管理员
        notice(session('旅社')['id'], 1, '有新的筹单需要审核', '有新的筹单需要审核，请查看');
        //跳转到特定的筹单完善页面
        $this->redirect($fundingType.'/update?id='.$fundingSpecifyId);
    }

    /**
     * 更新之后做的事情
     */
    public function interfaceAfterUpdate($id) {
        //如果用户已经签署，更新funding和CustomizedTravel状态
        $funding = D('Funding')->find($id);
        if (($funding['user_contract_state'] == '已签署')&&($funding['state'] == '待签署合同')) {
            //funding状态
            D('Funding')->where(['id' => $id])->data(['state' => '待支付'])->save();
            //定制旅行状态
            D('CustomizedTravel')->where(['funding_id' => $id])->data(['state' => '待支付'])->save();
        }

        if (session('firstSign')) {
            $this->redirect('Funding/list');
        }
        //查找当前筹单的具体筹单，跳转到具体筹单进行完善
        $funding = D('Funding')->find($id);
        $fundingType = D('FundingType')->find($funding['funding_type'])['en'];
        $fundingType = 'Funding'.ucfirst($fundingType);
        $fundingSpecifyId = D($fundingType)->where(['principal_id'=>$id])->find()['id'];
        $this->redirect($fundingType.'/update?id='.$fundingSpecifyId);
    }

    /**
     * 删除之前做的事情
     */
    public function interfaceBeforeDelete($id) {
    }

    /**
     * 更新之前做的事情
     */
    public function interfaceBeforeUpdate($id) {

    }

    /**
     * 父类中已经有典型的CRUD操作并预留入口
     * 如有较大改动则覆盖一下
     */
    public function create() {
        if ($_GET['transaction_type'] == '定制') {
            $_POST['state'] = '待签署合同';
            $_POST['name'] = '定制筹单';
        }
        parent::create();

    }

    public function delete() {
        parent::delete();
    }

    public function update($id) {
        //如果旅社第一次签署合同
        $funding = D('Funding')->find($id);
        if (($funding['seller_contract_state'] == '未签署') && ($_POST['seller_contract_state'] == '已签署')) {
            session('firstSign', true);
        } else {
            session('firstSign', false);
        }
        //如果合同内容有修改，修改买方为未签署
        if ($funding['seller_contract'] != $_POST['seller_contract']) {
            $_POST['user_contract_state'] = '未签署';
            // $_POST['seller_contract_state'] = '未签署';
        }
        // dump(session('旅社'));exit;
        $_POST['user_id'] = session('旅社')['id'];
        parent::update($id, $param);

    }

    public function list() {
        //源代码
        //$_GET['searchMap']['user_id'] = session('旅社')['id'];
        // parent::list();
       
        $list_tar = M('customized_travel')->where("club_user_id = {$_SESSION['旅社']['id']} and (transaction_type = '购买' or transaction_type='定制')")->order("id desc")->select();
        $list = array(); 
        foreach($list_tar as $k=>$v){
            $funding_info =  M('funding')->where("id = {$v['funding_id']}")->find();
            array_push($list,$funding_info);
        }
        $this->assign('list',array_filter ($list));
        $this->display();
    }

    public function detail($id = 0) {
        parent::detail($id);
    }

    public function import() {
        parent::import();
    }

    public function export() {
        parent::export();
    }




}
